<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible" width="60%">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
      label-width="110px">
      <el-form-item label="父级菜单" prop="parentId">
        <el-cascader v-model="dataForm.parentId" :options="menuList" :show-all-levels="false" :props="cascaderProps" clearable
          placeholder="请选择" style="width: 100%;"></el-cascader>
      </el-form-item>
      <el-form-item label="菜单名称" prop="title">
        <el-input v-model="dataForm.title" placeholder="菜单名称"></el-input>
      </el-form-item>
      <el-form-item label="路径" prop="path">
        <el-input v-model="dataForm.path" placeholder="路径"></el-input>
      </el-form-item>
      <el-form-item label="图标" prop="icon">
        <el-input v-model="dataForm.icon" placeholder="图标"></el-input>
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input v-model.number="dataForm.sort" placeholder="排序"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getDataInfo, savaDataInfo } from '@/api/menu';
export default {
  props: {
    menuList: {
      type: Array,
      default: () => {
        return [];
      },
    },
  },
  data() {
    return {
      visible: false,
      dataForm: {},
      dataRule: {
        title: [{ required: true, message: '菜单名称不能为空', trigger: 'blur' }],
        path: [{ required: true, message: '路径不能为空', trigger: 'blur' }],
      },
      cascaderProps: { value: 'id', label: 'title', emitPath: false, checkStrictly: true },
      // menuList: [
      //   { id: '1', menu: '首页' },
      //   { id: '2', menu: '接待管理' },
      // ]
    };
  },
  methods: {
    init(id) {
      this.visible = true;
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields();
        this.dataForm = { id: id, parentId: '0' };
        if (this.dataForm.id) {
          getDataInfo(this.dataForm.id)
            .then((res) => {
              if (res && res.code === 0) {
                this.dataForm = res.data;
              }
            })
            .catch((err) => {
              console.log(err);
            });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.dataForm.parentId = this.dataForm.parentId ? this.dataForm.parentId : '0';
          savaDataInfo(this.dataForm)
            .then((data) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false;
                    this.$emit('refreshDataList');
                  },
                });
              }
            })
            .catch((err) => {
              console.log(err);
            });
        }
      });
    },
  },
};
</script>
